Lecture 07 - More Git Commands, CLI and Git Practice
push and pull changes to and from remote repositories.gitignore to avoid tracking certain filesgit branch and git checkoutclone and fork repositoriesgit checkout and git resetSource: Lodato (2010)
Today we will cover:
git diffgit commit --amendgit cherry-pick 😅git rebase and its uses
git diff?git diff shows you what has changed between commits, branches, or your working directory+ for additions and - for deletionsmy-project repository01-data-cleaning.py filediff --git a/file b/file shows Git is comparing two versions of the same file (a = original, b = modified)--- a/file and +++ b/file indicate the original file (before) and new file (after) being compared@@ -0,0 +1 @@ shows the line numbers affected, with + lines indicating additions and - lines indicating deletions. -0,0 means that the original file had no lines, and +1 means one line was addedgit diff commandsBasic diff commands:
git diff: shows unstaged changes in working directorygit diff --staged: shows staged changes ready to commitgit diff HEAD: shows all changes since last commitgit diff --name-only: shows only filenames that changedComparing commits:
git diff commit1..commit2: compares two specific commitsgit diff branch1..branch2: compares two branchesgit diff --stat: shows summary of changes (files modified, insertions, deletions)@@ -1,4 +1,4 @@ means that in the original file, lines 1 to 4 were present, and in the new file, lines 1 to 4 are also present, but with some changesgit commit --amend?git commit --amend allows you to modify your last commitAmending the commit message:
Adding forgotten files:
Both together:
Important rules:
git revert insteadgit resetgit reset can move the HEAD pointer to a previous commit--soft keeps changes in the staging area--hard discards all changes after the specified commitgit cherry-pick?git cherry-pick allows you to pick specific commits from one branch and apply them to anothergit cherry-pickBasic cherry-pick:
# Cherry-pick a single commit
git cherry-pick abc1234
# Cherry-pick multiple commits
git cherry-pick abc1234 def5678
# Cherry-pick a range of commits
git cherry-pick abc1234..def5678After cherry-picking:
Handling conflicts:
# If conflicts occur during cherry-pick
git cherry-pick --continue # After resolving conflicts
git cherry-pick --abort # Cancel the cherry-pick
git cherry-pick --skip # Skip the current commitCommon use cases:
git rebasegit rebase allows you to move or combine commits to a new base commitInteractive rebase:
# Interactive rebase of last 3 commits
git rebase -i HEAD~3
# Interactive rebase to specific commit
git rebase -i abc1234Rebase onto another branch:
During interactive rebase you can:
pick: use the commit as-isreword: change the commit messageedit: modify the commit contentssquash: combine with previous commitdrop: remove the commit entirelyreorder: change commit ordergh) is the official command-line tool for GitHubWSL/Ubuntu:
# Install
(type -p wget >/dev/null || (sudo apt update && sudo apt install wget -y)) \
&& sudo mkdir -p -m 755 /etc/apt/keyrings \
&& out=$(mktemp) && wget -nv -O$out https://cli.github.com/packages/githubcli-archive-keyring.gpg \
&& cat $out | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \
&& sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \
&& sudo mkdir -p -m 755 /etc/apt/sources.list.d \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y
# Update (if already installed)
sudo apt update && sudo apt install gh -ymacOS (using Homebrew):
Repository operations:
Pull requests and issues:
# Create a new pull request
gh pr create --title "My PR" --body "Description of my PR"
# List pull requests
gh pr list
# View pull request details
gh pr view 123 --web
# Create a new issue
gh issue create --title "My Issue" --body "Description of my issue"
# List issues
gh issue list
# View issue details
gh issue view 456 --webPlease complete the following tasks:
git-practice and initialize it as a Git repositoryREADME.md with the content “# Git Practice Repository”src and inside it create an empty file named main.pyhotfixsrc directory, create three files using brace expansion: utils.js, utils.css, utils.htmltemp and inside it create a file named debug.log.gitignore file and add temp/ to itsrc/main.py to src/app.pydocs and copy README.md into it as guide.mdtemp directory and its contentsmain branch and merge the hotfix branchAfter completing these tasks, verify your work by checking the commit history and file structure.
Here are the answers to the practice quiz. Try to complete the quiz first before checking these answers!
mkdir git-practice && cd git-practice && git initecho "# Git Practice Repository" > README.mdmkdir src && touch src/main.pygit add . && git commit -m "Initial commit with README and main.py"git checkout -b hotfixtouch src/utils.{js,css,html}mkdir temp && touch temp/debug.logecho "temp/" > .gitignoregit add . && git commit -m "Add hotfix files and gitignore"mv src/main.py src/app.pymkdir docs && cp README.md docs/guide.mdrm -rf tempgit add . && git commit -m "Complete hotfix development"git checkout main && git merge hotfixgit log --onelineAdditional verification commands:
git log --onelinels -lagit branchcat .gitignore